﻿using HKB.Web;
using HKB.Web.Admin;
using HKB.Web.DataHelper;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class Admin_CM_Ad_Update : Admin, IRequiresSessionState
{
    private Shop_City _AD_Shop_City = null;
    protected DefaultProfile profile
    {
        get
        {
            return (DefaultProfile)this.Context.Profile;
        }
    }
    protected global_asax applicationInstance
    {
        get
        {
            return (global_asax)this.Context.ApplicationInstance;
        }
    }
    public string AreaAndSecondSortID
    {
        get;
        set;
    }
    public string IsAddOrUpdate
    {
        get;
        set;
    }
    private Shop_City AD_Shop_City
    {
        get
        {
            Shop_City result;
            if (this._AD_Shop_City != null)
            {
                result = this._AD_Shop_City;
            }
            else
            {
                result = new Shop_City();
            }
            return result;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (base.Request.QueryString["act"] == "add")
        {
            if (!base.IsPostBack)
            {
                this.AddTabel.Visible = true;
                this.UpdateTable.Visible = false;
                this.DefaultValue();
                this.GetAllProvince(this.ddlAddProvince);
            }
            this.IsAddOrUpdate = "add";
        }
        else
        {
            if (!base.IsPostBack)
            {
                this.AddTabel.Visible = false;
                this.UpdateTable.Visible = true;
                int ad_id = int.Parse(base.Request.QueryString["adid"]);
                this.GetAdvertisment(ad_id);
            }
            this.IsAddOrUpdate = "update";
        }
        if (!base.IsPostBack)
        {
            this.txtBeginTime.Attributes.Add("onclick", "SelectDate(" + this.txtBeginTime.UniqueID + ",'yyyy-MM-dd');");
            this.TxtEndTime.Attributes.Add("onclick", "SelectDate(" + this.TxtEndTime.UniqueID + ",'yyyy-MM-dd');");
        }
    }
    private void GetAllProvince(DropDownList tempP)
    {
        tempP.DataSource = this.AD_Shop_City.GetAllProvince();
        tempP.DataTextField = "Province";
        tempP.DataValueField = "Province";
        tempP.DataBind();
        tempP.Items.Insert(0, new ListItem("请选择省份...", ""));
    }
    private void GetAdvertisment(int ad_id)
    {
        CM_AD_Manage cM_AD_Manage = new CM_AD_Manage();
        DataTable adByID = cM_AD_Manage.GetAdByID(ad_id);
        if (adByID != null && adByID.Rows.Count > 0)
        {
            DataRow dataRow = adByID.Rows[0];
            this.UtxtAdName.Text = dataRow["AdName"].ToString();
            this.UddlLocation.SelectedValue = dataRow["AdLocation"].ToString();
            if (!string.IsNullOrEmpty(dataRow["Area"].ToString()) && dataRow["Area"].ToString() != "")
            {
                this.rblUpdateArea.SelectedValue = "3";
                this.ddlUpdateProvince.Visible = true;
                this.ddlUpdateCity.Visible = true;
                this.ddlUpdateArea.Visible = true;
                this.GetAllProvince(this.ddlUpdateProvince);
                foreach (ListItem listItem in this.ddlUpdateProvince.Items)
                {
                    if (listItem.Value == dataRow["Province"].ToString())
                    {
                        listItem.Selected = true;
                        break;
                    }
                }
                this.GetCityByProvince(this.ddlUpdateProvince, this.ddlUpdateCity, this.ddlUpdateArea);
                foreach (ListItem listItem in this.ddlUpdateCity.Items)
                {
                    if (listItem.Value == dataRow["City"].ToString())
                    {
                        listItem.Selected = true;
                        break;
                    }
                }
                this.GetAreaByCity();
                foreach (ListItem listItem in this.ddlUpdateArea.Items)
                {
                    if (listItem.Value == dataRow["Area"].ToString())
                    {
                        listItem.Selected = true;
                        break;
                    }
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(dataRow["City"].ToString()) && dataRow["City"].ToString() != "")
                {
                    this.rblUpdateArea.SelectedValue = "2";
                    this.ddlUpdateProvince.Visible = true;
                    this.ddlUpdateCity.Visible = true;
                    this.ddlUpdateArea.Visible = false;
                    this.GetAllProvince(this.ddlUpdateProvince);
                    foreach (ListItem listItem in this.ddlUpdateProvince.Items)
                    {
                        if (listItem.Value == dataRow["Province"].ToString())
                        {
                            listItem.Selected = true;
                            break;
                        }
                    }
                    this.GetCityByProvince(this.ddlUpdateProvince, this.ddlUpdateCity, this.ddlUpdateArea);
                    foreach (ListItem listItem in this.ddlUpdateCity.Items)
                    {
                        if (listItem.Value == dataRow["City"].ToString())
                        {
                            listItem.Selected = true;
                            break;
                        }
                    }
                    this.GetAreaByCity();
                }
                else
                {
                    if (!string.IsNullOrEmpty(dataRow["Province"].ToString()) && dataRow["Province"].ToString() != "")
                    {
                        this.rblUpdateArea.SelectedValue = "1";
                        this.ddlUpdateProvince.Visible = true;
                        this.ddlUpdateCity.Visible = false;
                        this.ddlUpdateArea.Visible = false;
                        this.GetAllProvince(this.ddlUpdateProvince);
                        foreach (ListItem listItem in this.ddlUpdateProvince.Items)
                        {
                            if (listItem.Value == dataRow["Province"].ToString())
                            {
                                listItem.Selected = true;
                                break;
                            }
                        }
                    }
                }
            }
            this.UtxtBeginDate.Text = dataRow["BeginTime"].ToString();
            this.UtxtEndDate.Text = dataRow["EndTime"].ToString();
            this.UtxtLinkUrl.Text = dataRow["LinkUrl"].ToString();
            this.UtxtLinkTip.Text = dataRow["LinkTip"].ToString();
            this.ImgUrlUpdate.Value = dataRow["ImgPath"].ToString();
            this.PrevImg.ImageUrl = dataRow["ImgPath"].ToString();
            this.UtxtIntro.Text = dataRow["AdIntro"].ToString();
            this.cbEnable.Checked = (Convert.ToInt32(dataRow["Enabled"].ToString()) == 1);
        }
    }
    protected void ddlAddProvince_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        this.GetCityByProvince(this.ddlAddProvince, this.ddlAddCity, this.ddlAddArea);
    }
    private void GetAreaByCity()
    {
        if (this.ddlUpdateCity.SelectedIndex == 0)
        {
            this.ddlUpdateArea.Items.Clear();
            this.ddlUpdateArea.Items.Add(new ListItem("请选择地区...", ""));
        }
        else
        {
            this.ddlUpdateArea.DataSource = this.AD_Shop_City.GetAllAreaByCity(this.ddlUpdateCity.SelectedValue);
            this.ddlUpdateArea.DataTextField = "Area";
            this.ddlUpdateArea.DataValueField = "Area";
            this.ddlUpdateArea.DataBind();
            if (this.ddlUpdateArea.Items.Count > 0)
            {
                for (int i = this.ddlUpdateArea.Items.Count - 1; i >= 0; i--)
                {
                    if (this.ddlUpdateArea.Items[i].Value == "" && this.ddlUpdateArea.Items[i].Text != "请选择地区...")
                    {
                        this.ddlUpdateArea.Items.RemoveAt(i);
                    }
                }
            }
            this.ddlUpdateArea.Items.Insert(0, new ListItem("请选择地区...", ""));
        }
    }
    protected void ddlUpdateProvince_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        this.GetCityByProvince(this.ddlUpdateProvince, this.ddlUpdateCity, this.ddlUpdateArea);
    }
    protected void ddlUpdateCity_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        this.GetAreaByCity();
    }
    private void GetCityByProvince(DropDownList tempP, DropDownList tempC, DropDownList tempA)
    {
        if (tempP.SelectedIndex == 0)
        {
            tempC.Items.Clear();
            tempC.Items.Add(new ListItem("请选择城市...", ""));
            tempC.Items.Clear();
            tempC.Items.Add(new ListItem("请选择地区...", ""));
        }
        else
        {
            tempC.DataSource = this.AD_Shop_City.GetCityByProvince(tempP.SelectedValue);
            tempC.DataTextField = "City";
            tempC.DataValueField = "City";
            tempC.DataBind();
            if (tempC.Items.Count > 0)
            {
                for (int i = tempC.Items.Count - 1; i >= 0; i--)
                {
                    if (tempC.Items[i].Value == "" && tempC.Items[i].Text != "请选择城市...")
                    {
                        tempC.Items.RemoveAt(i);
                    }
                }
            }
            tempC.Items.Insert(0, new ListItem("请选择城市...", ""));
            tempA.Items.Clear();
            tempA.Items.Add(new ListItem("请选择地区...", ""));
        }
    }
    protected void ddlAddCity_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.ddlAddCity.SelectedIndex == 0)
        {
            this.ddlAddArea.Items.Clear();
            this.ddlAddArea.Items.Add(new ListItem("请选择地区...", ""));
        }
        else
        {
            this.ddlAddArea.DataSource = this.AD_Shop_City.GetAllAreaByCity(this.ddlAddCity.SelectedValue);
            this.ddlAddArea.DataTextField = "Area";
            this.ddlAddArea.DataValueField = "Area";
            this.ddlAddArea.DataBind();
            if (this.ddlAddArea.Items.Count > 0)
            {
                for (int i = this.ddlAddArea.Items.Count - 1; i >= 0; i--)
                {
                    if (this.ddlAddArea.Items[i].Value == "" && this.ddlAddArea.Items[i].Text != "请选择地区...")
                    {
                        this.ddlAddArea.Items.RemoveAt(i);
                    }
                }
            }
            this.ddlAddArea.Items.Insert(0, new ListItem("请选择地区...", ""));
        }
    }
    private void DefaultValue()
    {
        this.ddlAddCity.Visible = false;
        this.ddlAddArea.Visible = false;
        this.txtBeginTime.Value = DateTime.Now.ToShortDateString();
        this.TxtEndTime.Text = DateTime.Now.AddMonths(1).ToShortDateString();
        this.txtLinkUrl.Text = "#";
        this.txtLinkTip.Text = "创美";
        this.cbEnable.Checked = true;
    }
    protected void rblAddArea_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadioButtonList radioButtonList = (RadioButtonList)sender;
        string selectedValue = radioButtonList.SelectedValue;
        if (selectedValue != null)
        {
            if (!(selectedValue == "1"))
            {
                if (!(selectedValue == "2"))
                {
                    if (selectedValue == "3")
                    {
                        this.ddlAddProvince.Visible = true;
                        this.ddlAddCity.Visible = true;
                        this.ddlAddArea.Visible = true;
                    }
                }
                else
                {
                    this.ddlAddProvince.Visible = true;
                    this.ddlAddCity.Visible = true;
                    this.ddlAddArea.Visible = false;
                }
            }
            else
            {
                this.ddlAddProvince.Visible = true;
                this.ddlAddCity.Visible = false;
                this.ddlAddArea.Visible = false;
            }
        }
    }
    protected void rblUpdateArea_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadioButtonList radioButtonList = (RadioButtonList)sender;
        string selectedValue = radioButtonList.SelectedValue;
        if (selectedValue != null)
        {
            if (!(selectedValue == "1"))
            {
                if (!(selectedValue == "2"))
                {
                    if (selectedValue == "3")
                    {
                        this.ddlUpdateProvince.Visible = true;
                        this.ddlUpdateCity.Visible = true;
                        this.ddlUpdateArea.Visible = true;
                    }
                }
                else
                {
                    this.ddlUpdateProvince.Visible = true;
                    this.ddlUpdateCity.Visible = true;
                    this.ddlUpdateArea.Visible = false;
                }
            }
            else
            {
                this.ddlUpdateProvince.Visible = true;
                this.ddlUpdateCity.Visible = false;
                this.ddlUpdateArea.Visible = false;
            }
        }
    }
    protected void ButAdd_Click(object sender, EventArgs e)
    {
        string selectedValue = this.rblAddArea.SelectedValue;
        if (selectedValue != null)
        {
            if (!(selectedValue == "1"))
            {
                if (!(selectedValue == "2"))
                {
                    if (selectedValue == "3")
                    {
                        if (this.ddlAddProvince.SelectedIndex == 0)
                        {
                            this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('请选择省份！');</script>");
                            return;
                        }
                        if (this.ddlAddCity.SelectedIndex == 0)
                        {
                            this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('请选择城市！');</script>");
                            return;
                        }
                        if (this.ddlAddArea.Items.Count > 1 && this.ddlAddArea.SelectedIndex == 0)
                        {
                            this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('请选择地区！');</script>");
                            return;
                        }
                    }
                }
                else
                {
                    if (this.ddlAddProvince.SelectedIndex == 0)
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('请选择省份！');</script>");
                        return;
                    }
                    if (this.ddlAddCity.SelectedIndex == 0)
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('请选择城市！');</script>");
                        return;
                    }
                }
            }
            else
            {
                if (this.ddlAddProvince.SelectedIndex == 0)
                {
                    this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('请选择省份！');</script>");
                    return;
                }
            }
        }
        if (this.Page.IsValid)
        {
            CM_AD_Manage cM_AD_Manage = new CM_AD_Manage();
            if (cM_AD_Manage.AddAdvertisment(this.txtAdName.Text.Trim(), this.ddlLocation.SelectedValue, this.ddlAddProvince.SelectedValue, this.ddlAddCity.SelectedValue, this.ddlAddArea.SelectedValue, -1, Convert.ToDateTime(this.txtBeginTime.Value), Convert.ToDateTime(this.TxtEndTime.Text), this.txtLinkUrl.Text.Trim(), this.txtLinkTip.Text.Trim(), this.ImgUrlAdd.Value, this.txtIntroduce.Text, this.cbEnable.Checked ? 1 : 0))
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>if(confirm('是否继续添加？')){location.href='CM_Ad_Update.aspx?act=add&MenuID=7&MenuName=服务&FileName=添加广告'}else{location.href='CM_Ad_Manage.aspx?MenuID=7&MenuName=服务&FileName=添加广告'}</script>");
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('新增失败！');</script>");
            }
        }
    }
    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        string selectedValue = this.rblUpdateArea.SelectedValue;
        if (selectedValue != null)
        {
            if (!(selectedValue == "1"))
            {
                if (!(selectedValue == "2"))
                {
                    if (selectedValue == "3")
                    {
                        if (this.ddlUpdateProvince.SelectedIndex == 0)
                        {
                            this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('请选择省份！');</script>");
                            return;
                        }
                        if (this.ddlUpdateCity.SelectedIndex == 0)
                        {
                            this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('请选择城市！');</script>");
                            return;
                        }
                    }
                }
                else
                {
                    if (this.ddlUpdateProvince.SelectedIndex == 0)
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('请选择省份！');</script>");
                        return;
                    }
                    if (this.ddlUpdateCity.SelectedIndex == 0)
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('请选择城市！');</script>");
                        return;
                    }
                }
            }
            else
            {
                if (this.ddlUpdateProvince.SelectedIndex == 0)
                {
                    this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('请选择省份！');</script>");
                    return;
                }
            }
        }
        if (this.Page.IsValid && this.UpdateCheckData())
        {
            CM_AD_Manage cM_AD_Manage = new CM_AD_Manage();
            if (cM_AD_Manage.UpdateAdvertisment(base.Request.QueryString["adid"], this.UtxtAdName.Text.Trim(), this.UddlLocation.SelectedValue, this.ddlUpdateProvince.SelectedValue, this.ddlUpdateCity.SelectedValue, this.ddlUpdateArea.SelectedValue, -1, Convert.ToDateTime(this.UtxtBeginDate.Text), Convert.ToDateTime(this.UtxtEndDate.Text), this.UtxtLinkUrl.Text.Trim(), this.UtxtLinkTip.Text.Trim(), this.ImgUrlUpdate.Value, this.UtxtIntro.Text, this.UcbEnable.Checked ? 1 : 0))
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>if(confirm('是否继续修改？')){location.href='CM_Ad_Update.aspx?act=update&adid=" + base.Request.QueryString["adid"] + "&MenuID=7&MenuName=服务&FileName=添加广告'}else{location.href='CM_Ad_Manage.aspx?MenuID=7&MenuName=服务&FileName=广告管理'}</script>");
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('修改失败！');</script>");
            }
        }
    }
    private bool UpdateCheckData()
    {
        bool result;
        if (Convert.ToDateTime(this.UtxtBeginDate.Text) > Convert.ToDateTime(this.UtxtEndDate.Text))
        {
            this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('开始时间不能大于结束时间！');</script>");
            result = false;
        }
        else
        {
            if (string.IsNullOrEmpty(this.ImgUrlUpdate.Value))
            {
                this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "<script>alert('图片还没有上传！');</script>");
                result = false;
            }
            else
            {
                result = true;
            }
        }
        return result;
    }
}
